package com.example.takeaway.api;

import com.example.takeaway.common.R;
import com.example.takeaway.model.dto.AddressDTO;
import com.example.takeaway.model.dto.AddressEditorDTO;
import com.example.takeaway.model.vo.AddressVO;
import com.example.takeaway.service.DeliveryAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/address")
public class DeliveryAddressApi {

    @Resource
    DeliveryAddressService deliveryAddressService;

    @PostMapping
    public R<Void> add(@RequestBody AddressDTO addressDTO) {
        //todo 从token中获取用户id
        deliveryAddressService.addAddress(addressDTO, 111L);
        return R.ok();
    }

    @PutMapping
    public R<Void> update(@RequestBody AddressEditorDTO addressDTO) {
        deliveryAddressService.updateAddress(addressDTO);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        deliveryAddressService.deleteById(id);
        return R.ok();
    }

    @GetMapping("list")
    public R<List<AddressVO>> list() {
        //todo 从token中获取用户id
        return R.ok(deliveryAddressService.queryListByUserId(111L));
    }

    @GetMapping("/{id}")
    public R<AddressVO> queryById(@PathVariable Long id) {
        return R.ok(deliveryAddressService.queryById(id));
    }
}
